//
//  DepositViewController.m
//  BusinessApp
//
//  Created by macox on 16/10/7.
//  Copyright © 2016年 macox. All rights reserved.
//

#import "DepositViewController.h"
#import "DepostModel.h"
#import "ModifyyViewController.h"
#import "MyWalleModel.h"
#import "CashRecordsViewController.h"
@interface DepositViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UILabel *PingTaiLabel;
@property (nonatomic , strong) NSString *AllMoney;

@end

@implementation DepositViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"提现";
    [self requestData];
    UIImage *classBackImage = [UIImage imageNamed:@"zuojiantou@3x-1"];
    classBackImage = [classBackImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:classBackImage style:UIBarButtonItemStylePlain target:self action:@selector(classBackAction)];
    
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithTitle:@"提现记录" style:UIBarButtonItemStylePlain target:self action:@selector(liushuizhangAction)];
    self.navigationItem.rightBarButtonItem = rightBarItem;
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    // Do any additional setup after loading the view from its nib.
   _AccountMoney.text = [NSString stringWithFormat:@"可用余额:%@元",_AccountMoeny];
    _AllMoney = _AccountMoeny;
//    UITextField *tf=[[UITextField alloc] init];
//    if ([_ShopIsDaiLi isEqualToString:@"1"]) {
//        _TrueMoney.textColor = [UIColor whiteColor];
//        _PingTaiLabel.textColor = [UIColor whiteColor];
//    }
    _TIXianMoney.delegate=self;
    // Do any additional setup after loading the view from its nib.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self requestData];
}
#pragma mark -- 银行卡信息
- (void)requestData{
    NSDictionary *param = @{@"ShouJiHao":GETPHONE,@"Duuid":GETUUID,@"Token":GETTOKEN};
    [GDJHttpTool get:GetMineBankInfo params:param success:^(id responseObj) {
        NSLog(@"%@", responseObj);
        NSLog(@"%@", param);
        if ([[responseObj objectForKey:@"Err"] intValue]) {
            
        }else{
            
            NSDictionary *dataArr = [responseObj objectForKey:@"BankAccountInfo"];
            
            MyWalleModel *model = [MyWalleModel mj_objectWithKeyValues:dataArr];
            
            _BrankName.text = model.BankName;
            
            _BrankNum.text =[NSString stringWithFormat:@"尾号为%@",[model.BankAccountNumber substringToIndex:4]];
            
        }
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];

}
#pragma mark -- 银行卡
- (IBAction)DepositBtnAction:(UIButton *)sender {
    NSLog(@"银行卡修改");
    ModifyyViewController *modifyVC = [[ModifyyViewController alloc]init];
    self.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:modifyVC animated:YES];
    self.hidesBottomBarWhenPushed=NO;

}
#pragma mark -- 确认提现
- (IBAction)makeSureDeposItBtnAction:(UIButton *)sender {
     NSLog(@"确认提现");
    NSUInteger intString = [_TIXianMoney.text intValue];
     NSUInteger intString1 = [_AccountMoeny intValue];
    
    
    if (intString > intString1 && intString1 > 10000) {
        NSLog(@"提现金额超出已有现金");
         [SVProgressHUD showErrorWithStatus:@"提现金额超出已有现金" ];
    }
    else{
        
        NSDictionary *param = @{@"ShouJiHao":GETPHONE,@"Duuid":GETUUID,@"Token":GETTOKEN,@"JinE":_TIXianMoney.text};
        
        
        [GDJHttpTool post:TiXianShenQing params:param success:^(id responseObj) {
            
            NSLog(@"====%@", responseObj);
            NSLog(@"%@", param);
            if (0 == [[responseObj valueForKey:@"Err"]integerValue] ) {
                [SVProgressHUD showSuccessWithStatus:@"您的提现申请已提交,请等待审核!" ];
            }
            if (703 == [[responseObj valueForKey:@"Err"]integerValue] ) {
                [SVProgressHUD showErrorWithStatus:@"提现说明:可用余额大于等于1000元时才可以提现" ];
            }

            
           _AccountMoney.text = [NSString stringWithFormat:@"可用余额:%@元",[responseObj valueForKey:@"Yue"]];
            _AllMoney = [NSString stringWithFormat:@"%@",[responseObj valueForKey:@"Yue"]];
            
        } failure:^(NSError *error) {
            
            
        }];
    }
   
   

}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];
    _TrueMoney.text = [NSString stringWithFormat:@"实际到账金额%.2f元", [textField.text intValue]  * 0.993];
    return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    
    if (textField == _TIXianMoney) {
    _TrueMoney.text = [NSString stringWithFormat:@"实际到账金额%.2f元", [textField.text intValue]  * 0.993];
    }
    
    
}
#pragma mark -- 限制输入数字和小数点
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    //控制长度
    if(range.location>=20)
        return NO;
    
    const char * ch=[string cStringUsingEncoding:NSUTF8StringEncoding];
    if(*ch == 0)
        return YES;
    //字符0－9 和 .
    if( *ch != 46 && ( *ch<48 || *ch>57) )
        return NO;
    
    //有了小数点
    if([textField.text rangeOfString:@"."].length==1)
    {
        NSUInteger length=[textField.text rangeOfString:@"."].location;
        
        //小数点后面两位小数 且不能再是小数点
        if([[textField.text substringFromIndex:length] length]>3 || *ch ==46)   //3表示后面小数位的个数。。
            
            return NO;
    }
    
    return YES;
    
}

#pragma mark -- 返回
- (void)classBackAction{
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)AllTiXianBtn:(UIButton *)sender {
    
    _TIXianMoney.text = _AllMoney;
}

#pragma mark -- 流水账
- (void)liushuizhangAction{
    CashRecordsViewController  *datAccountVC = [[CashRecordsViewController alloc]init];
    self.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:datAccountVC animated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
